package net.xxcxcxcx.xc_enhancedmob.skills.basics.individual.mystery;

import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.xxcxcxcx.xc_enhancedmob.config.skill.SkillConfig;
import net.xxcxcxcx.xc_enhancedmob.skills.BaseSkill;
import net.xxcxcxcx.xc_enhancedmob.skills.Ref;
import net.xxcxcxcx.xc_enhancedmob.skills.SkillRarity;

import java.util.Random;

public class VisciditySkill extends BaseSkill {

    public VisciditySkill(int level){super(level);}

    public VisciditySkill(NbtCompound nbtCompound){super(nbtCompound);}

    public VisciditySkill(){
    }

    @Override
    public SkillConfig getDefaultConfig() {
        SkillConfig skillConfig = super.getDefaultConfig();
        skillConfig.setMaxlevel(10);
        skillConfig.setMessage(getConfigMessage(skillConfig));
        return super.getDefaultConfig();
    }

    @Override
    public String getConfigMessage(SkillConfig skillConfig) {
        return "每次受到攻击时有一定几率将攻击者的武器丢出";
    }

    public SkillRarity getSkillRarity(){return  SkillRarity.Mystery;}

    @Override
    public boolean onDamaged(LivingEntity attacker, MobEntity target, DamageSource source, Ref<Float> amount, boolean ismob, boolean remote) {
        if(attacker != null && !source.isSourceCreativePlayer() && !source.isProjectile())
        {
            Random random = new Random();
            int num = random.nextInt(100)+1;
            if(num <= 30+7*getSkillLevel())
            {
                if(!attacker.getEquippedStack(EquipmentSlot.MAINHAND).isEmpty())
                {
                    ItemStack Equip = attacker.getEquippedStack(EquipmentSlot.MAINHAND);
                    ItemStack itemStack = Equip.copy();
                    itemStack.setCount(1);
                    attacker.dropStack(itemStack);
                    Equip.split(1);
                }
                if(!attacker.getEquippedStack(EquipmentSlot.OFFHAND).isEmpty())
                {
                    ItemStack Equip = attacker.getEquippedStack(EquipmentSlot.OFFHAND);
                    ItemStack itemStack = Equip.copy();
                    itemStack.setCount(1);
                    attacker.dropStack(itemStack);
                    Equip.split(1);
                }
            }
        }
        return true;
    }
}
